package com.stadium.web.service.impl;

import com.fastsite.common.enums.StatusEnum;
import com.fastsite.dao.utils.PageSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import com.stadium.web.repository.MarketActivityRepository;
import com.stadium.web.domain.MarketActivity;
import com.stadium.web.service.MarketActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
/**
 * 营销活动Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-03-15
 */
@Service
public class MarketActivityServiceImpl implements MarketActivityService
{
    @Autowired
    private MarketActivityRepository marketActivityRepository;

    /**
     * 根据营销活动ID查询营销活动数据
     * @param id 营销活动ID
     */
    @Override
    @Transactional
    public MarketActivity getById(Long id) {
        return marketActivityRepository.findById(id).orElse(null);
    }

    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<MarketActivity> getPageList(Example<MarketActivity> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest(null,Sort.Direction.ASC);
        return marketActivityRepository.findAll(example, page);
    }


    /**
     * 保存营销活动
     * @param marketActivity 营销活动 实体类
     */
    @Override
    public MarketActivity save(MarketActivity marketActivity){
        return marketActivityRepository.save(marketActivity);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> ids){
        // 删除营销活动时取消与营销活动和菜单的关联
        return marketActivityRepository.updateStatusByIdIn(statusEnum.getCode(), ids) > 0;
    }

    @Override
    public List<MarketActivity> findByVenueId(Long id) {
        return marketActivityRepository.findByVenueId(id);
    }

}
